﻿$(function () {
    $.kish.AproveCarStore = {};
    $.extend($.kish.AproveCarStore, {
        WithTotal: null,
        initializing: function () {
            $.kish.AproveCarStore.ListCarStore(0, 20, true);

            $(".AprCarStoreSubmit").click(function () {
                $.kish.AproveCarStore.ApproveCarStoreList();
            });
            $(".CarStoreSearchSubmit").click(function () {
                $.kish.AproveCarStore.ListCarStore(0, 20, true);
            });
        },

        //*************************//
        ListCarStore: function (start, end, withtotal) {
            var StoreName = $('.storeNameTxt').val();
            $.ajax({
                url: '../../KishCar/Services/CarAnn.svc/GetInActiveCarStore',
                data: JSON.stringify({ StoreName: StoreName, Start: start, End: end, WithTotal: withtotal }),
                dataType: "json",
                type: "POST",
                contentType: "application/json; charset=utf-8",
                success: function (data) {

                    if (data['d'] != null) {
                        if (data['d'].length == 0) {
                            $.kish.message.getErrorMessage("15");
                        }
                        else {
                            $('.carStoreTable').html('');
                            var tableHeader = '<tr><th >تصویر</th><th >نام</th><th >نام مدیر</th><th >تلفن</th><th>آدرس</th><th>پست الکترونیکی</th><th>توضیحات</th><th>تایید</th><th><input type="checkbox" class="chkSelectAll"/></th></tr>';
                            $('.carStoreTable').append(tableHeader);
                            for (var i = 0; i < data['d'].Data.length; i++) {
                                var path = '';
                                if (data['d'].Data[i].File != null) {
                                    path = (data['d'].Data[i].File.Path);
                                    var relatedPath = '../' + path.replace('~', '..');
                                    var Image = '<a  class="highslide" href="' + relatedPath + '"> <img  class="width80 fn_prodImg " alt="' + data['d'].Data[i].Name + '" title="تصویر بزرگتر" src= "' + relatedPath + '"/></a>';
                                }
                                var x = '<tr id=' + data['d'].Data[i].ID + '><td >' + (path ? Image : "بدون تصویر") + ' </td><td class=" breakWord">' + data['d'].Data[i].Name + ' </td ><td>' + data['d'].Data[i].ManagerName + '</td><td>' + data['d'].Data[i].Telephone + '</td><td><p>' + data['d'].Data[i].Address + '</p></td><td>' + data['d'].Data[i].Email +'</td>'+                                
                                '<td><textarea rows="3" cols="20" name="comments" class="commentTxt" readonly="readonly">'+data['d'].Data[i].Description+'</textarea></td>'+
                                '<td><img alt="تایید" title="تایید" src="../../Images/ico/tick.png" class="noBorder inline-block pointer fn_Apr" /></td><td><input type="checkbox" id="' + data['d'].Data[i].ID + '" class="chk"/></td></tr>';
                                $('.carStoreTable').append(x);
                                $('.carStoreTable tr:last').data('DTO', data['d'].Data[i])
                            };

                            $('.fn_Apr').click(function () {
                                var thisRow = $(this).closest("tr");
                                $.kish.AproveCarStore.ApproveCarStore(thisRow);
                            });

                            $('.fn_ApproveDetail').click(function () {
                                var carAnnID = $(this).closest("tr").attr('id');
                                var isInCarStore = $(this).closest("tr").data("DTO").IsInCarStore;
                                window.location.replace("./ApproveAnnDetail.aspx?ID=" + carAnnID+"&CarStore="+isInCarStore);
                            });

                            $('a.highslide').click(function () {
                                return hs.expand(this)
                            });

                            if (withtotal && data['d'].TotalCount && data['d'].TotalCount > 0 && data['d'].TotalCount != null) {
                                $.kish.AproveCarStore.WithTotal = data['d'].TotalCount;
                                $.kish.AproveCarStore.SetPaging();
                            }
                         
                           $('.chkSelectAll').click(function () {
                            $('.chk').attr('checked', this.checked);
                            });
                          
                            $(".chk").click(function () {

                                if ($(".chk").length == $(".chk:checked").length) {
                                    $(".chkSelectAll").attr("checked", "checked");
                                } else {
                                    $(".chkSelectAll").removeAttr("checked");
                                }

                            });
                        }
                    }
                }
            });
        },

        SetPaging: function () {
            var withTotal = $.kish.AproveCarStore.WithTotal;
            var PageCount = Math.ceil(withTotal / 20);
            var paging = '';
            for (var i = 0; i < PageCount; i++) {
                var PageItem = '<span class="pagingItem">' + (i + 1).toString() + '</span>';
                paging = paging + PageItem;
            }
            $('.PagingDiv').text('');
            $('.PagingDiv').append(paging);
            $('.PagingDiv').append('<span class="totalCount"> کل موارد یافت شده : ' + withTotal + '</span>');
            $('.pagingItem', '.PagingDiv').click(function () {
                var pageIndex = $(this).text();
                $.kish.AproveCarStore.ListCarStore((pageIndex - 1) * 20, pageIndex * 20, false);
            });
        },
        //*************************//
        ApproveCarStore: function (thisRow) {
            var CarStoreID = thisRow.attr('id');
            $.kish.message.getConfirmationMessage(2, function () {

                $.ajax({
                    'type': 'POST',
                    'url': '../Services/CarAnn.svc/ActivatCarStore',
                    'data': JSON.stringify({ "CarStoreID": CarStoreID}),
                    'contentType': 'application/json; charset=utf-8',
                    'dataType': 'json',
                    'success': function (result) {
                        if (result['d'].IsSucceed) {
                            $(thisRow).remove();
                            // $.kish.ApproveCarAnn.ListCarAnn();
                            $.kish.message.showUserMessage(result['d'].Message.Message);
                        }
                        else {
                            $.kish.message.showErrorMessage(result['d'].Message.Message);
                        }
                    },
                    'error': function () {
                        $.kish.message.alertError("خطا در سیستم");
                    }
                });

            }, function () {
                // $this.show();
            });
        },
         ApproveCarStoreList: function () {
            var CarStoreApprove = new Array();
            $.each($('.chk'),function (){
            if($(this).is(':checked'))
            {
                CarStoreApprove.push($(this).attr('id'));
            }
            });
            $.kish.message.getConfirmationMessage(2, function () {
                $.ajax({
                    'type': 'POST',
                    'url': '../Services/CarAnn.svc/ActivatCarStores',
                    'data': JSON.stringify({ "CarStoreIDs": CarStoreApprove }),
                    'contentType': 'application/json; charset=utf-8',
                    'dataType': 'json',
                    'success': function (result) {
                        if (result['d'].IsSucceed) {
                           // $(thisRow).remove();
                             $.each($('.chk'),function (){
                                    if($(this).is(':checked'))
                                    {
                                        $(this).closest("tr").remove();
                                    }
                                    });
                            // $.kish.ApproveCarAnn.ListCarAnn();
                            $.kish.message.showUserMessage(result['d'].Message.Message);
                        }
                        else {
                            $.kish.message.showErrorMessage(result['d'].Message.Message);
                        }
                    },
                    'error': function () {
                        $.kish.message.alertError("خطا در سیستم");
                    }
                });

            }, function () {
                // $this.show();
            });
        },
    });
    $.kish.AproveCarStore.initializing();
});